package com.base.mail;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import com.base.utils.PropertiesUtil;
import com.base.utils.StringUtil;

/**
 * <p>发送邮件抽象基类</p>
 * @author Jason
 */
public abstract class SendMail {
	private String filePath;
	private String charset;
	private String hostName;
	private String userName;
	private String name;
	private String password;
	
	public void setFilePath(String filePath) {
		this.filePath = filePath;
		PropertiesUtil propertiesUtil = new PropertiesUtil(this.filePath);
		String charset = propertiesUtil.getProperty("mail.charset");
		String hostName = propertiesUtil.getProperty("mail.hostname");
		String userName = propertiesUtil.getProperty("mail.from");
		String name = propertiesUtil.getProperty("mail.name");
		String password = propertiesUtil.getProperty("mail.password");
		this.charset = charset;
		this.hostName = hostName;
		this.userName = userName;
		this.name = name;
		this.password = password;
	}

	protected abstract void sendMail(SendMailParam mailParam);
	
	protected void send(SendMailParam mailParam) {
		HtmlEmail email = new HtmlEmail();
		email.setTLS(true);
		email.setSSL(true);
		email.setCharset(charset);
		email.setHostName(hostName);//设置发信的smtp服务器  
		try {
			String sendToList = mailParam.getSendToList();
			String sendToName = mailParam.getSendToName();
			String[] sendUserList = sendToList.split(";");
			String[] sendNameList = null;
			List list = new ArrayList();
			if(StringUtil.isNotEmpty(sendToName)) 
				sendNameList = sendToName.split(";");
			for (int i = 1; i <= sendUserList.length; i++) {
				//设置收件人帐号和收件人  
				if(StringUtil.isNotEmpty(sendToName))
					list.add(createInternetAddress(sendUserList[i-1], sendNameList[i-1], charset));//设置收件人
				else
					list.add(createInternetAddress(sendUserList[i-1], null, charset));//设置收件人
				
				if(i % mailParam.getBccNum() == 0) {
					email.addTo(userName);//设置收件人
					email.setFrom(userName, name);//设置发信的邮件帐号和发信人  
					email.setSubject(mailParam.getSubject());//设置邮件主题  
					email.setAuthentication(userName, password);//如果smtp服务器需要认证的话，在这里设置帐号、密码  
					email.setHtmlMsg(mailParam.getSendContent());//设置邮件正文和字符编码  
					email.send();
				}
			}
			if(email.getBccAddresses().size() > 0) {
				email.addTo(userName);//设置收件人
				email.setFrom(userName, name);//设置发信的邮件帐号和发信人  
				email.setSubject(mailParam.getSubject());//设置邮件主题  
				email.setAuthentication(userName, password);//如果smtp服务器需要认证的话，在这里设置帐号、密码  
				email.setHtmlMsg(mailParam.getSendContent());//设置邮件正文和字符编码  
				email.send();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	protected void sendSinglen(SendMailParam mailParam) {
		HtmlEmail email = new HtmlEmail();
		email.setTLS(true);
		email.setSSL(true);
		email.setCharset(charset);
		email.setHostName(hostName);//设置发信的smtp服务器  
		try {
			String sendToList = mailParam.getSendToList();
			String sendToName = mailParam.getSendToName();
			String[] sendUserList = sendToList.split(";");
			String[] sendNameList = null;
			List list = new ArrayList();
			if(StringUtil.isNotEmpty(sendToName)) 
				sendNameList = sendToName.split(";");
			for (int i = 1; i <= sendUserList.length; i++) {
				//设置收件人帐号和收件人  
				if(StringUtil.isNotEmpty(sendToName))
					list.add(createInternetAddress(sendUserList[i-1], sendNameList[i-1], charset));//设置收件人
				else
					list.add(createInternetAddress(sendUserList[i-1], null, charset));//设置收件人
					email.addTo(sendUserList[i-1]);//设置收件人
					email.setFrom(userName, name);//设置发信的邮件帐号和发信人  
					email.setSubject(mailParam.getSubject());//设置邮件主题  
					email.setAuthentication(userName, password);//如果smtp服务器需要认证的话，在这里设置帐号、密码  
					email.setHtmlMsg(mailParam.getSendContent());//设置邮件正文和字符编码  
					email.send();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * 创建网络地址
	 * @param email
	 * @param name
	 * @param charsetName
	 * @return
	 * @throws EmailException
	 */
	protected InternetAddress createInternetAddress(String email, String name, String charsetName) throws EmailException
    {
     InternetAddress address = null;
     try {
       address = new InternetAddress(email);
       if (!StringUtil.isNotEmpty(name)) {
         name = email;
       }
       if (!StringUtil.isNotEmpty(charsetName)) {
         address.setPersonal(name);
       }
       else {
         Charset set = Charset.forName(charsetName);
         address.setPersonal(name, set.name());
       }
 
       address.validate();
     }
     catch (AddressException e) {
       throw new EmailException(e);
     }
     catch (UnsupportedEncodingException e) {
       throw new EmailException(e);
     }
     return address;
   }
	
	public static void main(String[] args) {
		SendMail mail = new SendMailHandler();
		SendMailParam mailParam = new SendMailParam();
		mailParam.setSubject("测试邮件");
		mailParam.setSendContent("亲们！");
		mailParam.setSendToList("514737546@qq.com;xuuuuuujian@gmail.com");
		mail.setFilePath("com/base/mybatis/config/jdbc.properties");
		mail.sendSinglen(mailParam);
	}
}